package cn.phantom.interrrupt;

import java.util.concurrent.TimeUnit;

public class InterruptTest04 {
    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("------is Interrupted = true, 程序结束");
                    break;
                }
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt(); // why add this line ?????
                    // Then its interrupt status will be cleared and it will receive an InterruptedException
                    // 线程的中断标志位变为false，需要再次调用interrupt使其状态变为true
                    e.printStackTrace();
                }
                System.out.println("------hello");
            }
        }, "t1");

        t1.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            t1.interrupt();
        }).start();


    }
}
